const { promisify } = require("util")
const figlet = promisify(require("figlet"))
const clear = require("clear")
const chalk = require("chalk")
const log = content =>  console.log(chalk.green(content))
const { clone } = require("./download")
const open = require("open")

const spawn = async (...args) => {
  const { spawn } = require("child_process")
  return new Promise(resolve => {
    const proc = spawn(...args)
    proc.stdout.pipe(process.stdout) // 正常流
    proc.stderr.pipe(process.stderr) // 异常流
    proc.on("close", () => {
      resolve()
    })
  })
}



module.exports = async name => {

  // 打印欢迎界面
  clear()
  const data = await figlet("CLI Welcome")
  log(data)
  log("🚀创建项目" + name)
  // 克隆远程仓库
  await clone("github:su37josephxia/vue-template", name)
  // 安装依赖 npm i => 子进程执行shell
  log("安装依赖")
  await spawn('npm', ['install'], { cwd: `./${name}`, shell: true })
  log(chalk.green(` 
    👌安装完成： To get Start: 
    =========================== 
    cd ${name} npm run serve 
    =========================== 
  `))
  // 打开浏览器 
  // open(`http://localhost:8080`); 
  // await spawn('npm', ['run', 'serve'], { cwd: `./${name}`, shell: true})
}















